import screenshot_adb
import padocr
import operation
import coordinate
import time

def go_alone(id = 2, device_serial=None):
    operation.adb_tap_with_delay(coordinate.coord['小秘书'], device_serial=device_serial) # 打开小秘书
    operation.adb_tap_with_delay(coordinate.coord['特殊传送'], device_serial=device_serial) # 点击特殊传送
    if id == 1:
        operation.adb_tap_with_delay(coordinate.coord['孤独之地一'], device_serial=device_serial) # 点击孤独一
    elif id == 2:
        operation.adb_tap_with_delay(coordinate.coord['孤独之地二'], device_serial=device_serial) # 点击孤独之地二

    operation.adb_tap_with_delay(coordinate.coord['孤独之地-我要进入'], device_serial=device_serial)
    time.sleep(0.5)
    operation.click_target_image(r"img\guaji.bmp")





if __name__ == '__main__':
    client = operation.connect_emulator()
    devices = client.devices()
    if not devices:
        print("没有找到连接的设备")
        exit()

    # 打印所有设备信息
    print("可用设备:")
    for i, device in enumerate(devices):
        print(f"{i}: {device.serial}")

    # 自动选择第一个设备（或让用户选择）
    selected_device = devices[1]
    print(f"\n选择设备: {selected_device.serial}")
    selected_device1 = devices[2]
    selected_device2 = devices[3]


    while True:
        # map_info = padocr.get_map_info(selected_device.serial)
        # map_info1 = padocr.get_map_info(selected_device1.serial)
        # map_info2 = padocr.get_map_info(selected_device2.serial)
        # operation.use_item(devices_serial=selected_device.serial)
        # operation.use_item(devices_serial=selected_device1.serial)
        # operation.use_item(devices_serial=selected_device2.serial)

        # print('开始循环')
        # time.sleep(1)
        mengzhong_tag0 = screenshot_adb.find_image_on_screen_adb(r"img\mengzhong.bmp",device_serial=selected_device.serial)
        mengzhong_tag1 = screenshot_adb.find_image_on_screen_adb(r"img\mengzhong.bmp",device_serial=selected_device1.serial)
        mengzhong_tag2 = screenshot_adb.find_image_on_screen_adb(r"img\mengzhong.bmp",device_serial=selected_device2.serial)
        #  第一个窗口
        # if map_info[1] == '全体':
        if mengzhong_tag0:
            time.sleep(100)
            go_alone(id = 2, device_serial=selected_device.serial)
            time.sleep(1)
        operation.close_all(devices_serial=selected_device.serial)
        operation.attack_empty(devices_serial=selected_device.serial)
        #第二个窗口
        # if map_info1[1] == '全体':
        if mengzhong_tag2:
            time.sleep(100)
            operation.go_shen_long(device_serial=selected_device2.serial)
            time.sleep(1)
        operation.close_all(devices_serial=selected_device2.serial)
        operation.attack_empty(devices_serial=selected_device2.serial)
        #第三个窗口
        # if map_info2[1] == '全体':
        if mengzhong_tag1:
            time.sleep(100)
            go_alone(id = 1, device_serial=selected_device1.serial)
            time.sleep(1)

            operation.close_all(devices_serial=selected_device1.serial)
            operation.attack_empty(devices_serial=selected_device1.serial)

